//
//  LBMeFeedBackController.m
//  loveBeen
//
//  Created by L.Hawk on 2016/11/20.
//  Copyright © 2016年 loveBeen. All rights reserved.
//

#import "LBMeFeedBackController.h"

@interface LBMeFeedBackController ()<UITextViewDelegate>
@property(nonatomic,weak)UITextView *textView;
@property(nonatomic,weak)UILabel *label;
@end

@implementation LBMeFeedBackController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor colorWithRed:225/255.0 green:225/255.0 blue:225/255.0 alpha:1];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"发送" style: UIBarButtonItemStyleDone target:self action:nil];
    self.navigationItem.title = @"意见反馈";
    [self setupUI];
    // Do any additional setup after loading the view.
}


-(void)setupUI{
    
    //创建textview内的label显示站位
    UILabel *label = [[UILabel alloc] init];
    label.text = @"请输入宝贵意见(300字以内)";
    label.textColor = [UIColor colorWithRed:208/255.0 green:208/255.0 blue:208/255.0 alpha:1];
    self.label = label;
    
    //1.创建提示文字
    //    CGRect rect = CGRectMake(25, 80, 325, 60);
    UILabel *stateLabel = [[UILabel alloc]init];
    [self.view addSubview:stateLabel];
    
    //设置和读取文本内容(默认为nil)
    stateLabel.text = @"您的批评和建议能帮助我们更好地完善产品, 请留下您的宝贵意见!";
    //设置标签文本对齐方式
    stateLabel.textAlignment = NSTextAlignmentLeft;
    //标签最多显示的行数
    stateLabel.numberOfLines = 3;
    [stateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(25);
        make.right.equalTo(self.view).offset(-25);
        make.top.equalTo(self.view).offset(80);//应改为距离navBar偏移
        make.height.mas_equalTo(60);
    }];
    
    
    //2.创建输入框
    UITextView * textView = [[UITextView alloc]init];
    [self.view addSubview:textView];
    //设置代理方法的实现类
    textView.delegate = self;
    //设置字体大小
    textView.font=[UIFont fontWithName:@"Arial" size:18.0];
    //设置键盘类型
    textView.keyboardType = UIKeyboardTypeDefault;
    //设置return键类型
    textView.returnKeyType=UIReturnKeyDone;
    //设置约束
    [textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(25);
        make.right.equalTo(self.view).offset(-25);
        make.top.equalTo(stateLabel.mas_bottom).offset(25);
        make.height.mas_equalTo(200);
    }];
    _textView = textView;
    
    [textView addSubview:label];
    
    //约束label
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(textView).offset(8);
    }];
}


//代理方法设置第一响应者   用来弹出键盘
-(BOOL)textViewShouldBeginEditing:(UITextView *)textView{
    //    [self.textView becomeFirstResponder];
    return YES;
}






- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(void)viewWillAppear:(BOOL)animated {
    //屏蔽统一navigationBar的样式，自定义样式
    [self.navigationController.navigationBar setTintColor:[UIColor grayColor]];
    [self.navigationController.navigationBar setBarTintColor:[UIColor whiteColor]];
}




- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    if (![self.textView isExclusiveTouch]) {
        [self.textView resignFirstResponder];
    }
}







-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if (range.location >= 300) {//检测到“完成”
        [textView resignFirstResponder];//释放键盘
        return NO;
    }
    if (textView.text.length==0){//textview长度为0
        if ([text isEqualToString:@""]) {//判断是否为删除键
            self.label.hidden=NO;//隐藏文字
        }else{
            self.label.hidden=YES;
        }
    }else{//textview长度不为0
        if (textView.text.length==1){//textview长度为1时候
            if ([text isEqualToString:@""]) {//判断是否为删除键
                self.label.hidden=NO;
            }else{//不是删除
                self.label.hidden=YES;
            }
        }else{//长度不为1时候
            self.label.hidden=YES;
        }
    }
    return YES;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
